﻿Imports System.Xml
Imports System.Reflection
Imports Mysoft.Map.Workflow.Types
Imports Mysoft.Map.Workflow.BusinessDomain
Imports Mysoft.Map.Data
Imports System.Web
Imports System.Data
Imports System.Data.SqlClient


Public Class ProceedPlanAdjustSP

    Inherits BusinessDomainBase

    Private _businessGUID As String     '业务GUID
    Private _xml As XmlDocument = New XmlDocument

    Public Sub New()
        MyBase.New("/PubProject/workflow/qmys/ProceedPlanAdjustSP.xml")
        _xml.LoadXml(MyBase.XML)
    End Sub

    '属性：业务GUID
    Public Overrides Property BusinessGUID() As String
        Get
            Return _businessGUID
        End Get
        Set(ByVal Value As String)
            _businessGUID = Value
        End Set
    End Property

    '属性：XML
    '最终XML格式：

    Public Overrides Property XML() As String
        Get
            Return _xml.OuterXml
        End Get
        Set(ByVal Value As String)
            _xml.LoadXml(Value)
        End Set
    End Property

    Public Overrides Function Initiate() As String
        Dim strSQL As String = "UPDATE ys_DeptAdjust SET ApproveState='审核中' WHERE DeptAdjustGUID='" & Me._businessGUID & "'"
        '提交更新语句
        If MyDB.ExecSQL(strSQL) < 0 Then
            Return "<xml result=""false"" errormessage=""审批失败，请稍后再试！""></xml>"
        End If

        '返回执行是否成功的信息
        Return "<xml result=""true"" errormessage=""""></xml>"
    End Function
    '功能：取业务数据，填充XML
    Public Overrides Function LoadData() As Integer
        Dim strSQL, strYsCdg, strYsCqn As String
        Dim dtTemp As DataTable

        '主记录    
        strSQL = "SELECT DeptAdjustGUID,Subject,DeptGUID,Year,vys_myBusinessUnit.BUName,Adjuster,AdjustDate,AdjustReason,ApproveState,ys_DeptAdjust.adjustamount,ys_DeptAdjust.Remarks " & _
                 " FROM ys_DeptAdjust left join vys_myBusinessUnit on ys_DeptAdjust.deptguid=vys_myBusinessUnit.buguid " & _
                 " WHERE DeptAdjustGUID='" & Me._businessGUID & "'"

        dtTemp = MyDB.GetDataTable(strSQL)

        If dtTemp.Rows.Count > 0 Then
            '根节点业务GUID属性
            _xml.SelectSingleNode("/BusinessType").Attributes.GetNamedItem("BusinessGUID").Value = Me._businessGUID

            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='预算调整单据GUID']").InnerText = dtTemp.Rows(0).Item("DeptAdjustGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='部门GUID']").InnerText = dtTemp.Rows(0).Item("DeptGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='部门名称']").InnerText = dtTemp.Rows(0).Item("BUName").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='主题']").InnerText = dtTemp.Rows(0).Item("Subject").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='年份']").InnerText = dtTemp.Rows(0).Item("Year").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='调整人名称']").InnerText = dtTemp.Rows(0).Item("Adjuster").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='调整日期']").InnerText = dtTemp.Rows(0).Item("AdjustDate").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='调整原因']").InnerText = dtTemp.Rows(0).Item("AdjustReason").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='申请人']").InnerText = HttpContext.Current.Session("UserName")
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='调整总金额']").InnerText = Format(dtTemp.Rows(0).Item("adjustamount"), "#,##0.00")
        End If

        Return 0
    End Function

    '功能：根据XML进行数据归档
    Public Overrides Function SaveData() As String
        Dim MyPublicMng As New PublicMNG
        '获取审核人、审核日期的审核抬头信息
        Dim strApproveSeparator As String = MyPublicMng.GetApproveSeparator("审批通过")
        Dim i As Integer
        Dim dtTemp As DataTable
        Dim strSQL As String = "UPDATE ys_DeptAdjust SET ApproveState='已审核'" & _
                               ",ApproveBy='" & HttpContext.Current.Session("UserName") & "'" & _
                               ",ApproveDate=GetDate()" & _
                               ",Remarks='" & strApproveSeparator & Chr(13) & Chr(10) & "    工作流审批通过" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "' + Convert(varchar(6000), IsNull(Remarks, '')) " & _
                               " WHERE DeptAdjustGUID='" & Me._businessGUID & "';"
        Dim strSQLALL As New System.Text.StringBuilder
        If MyDB.ExecSQL(strSQL) < 0 Then
            Return "<xml result=""false"" errormessage=""审批失败，请稍后再试！""></xml>"
        End If
        strSQL = "SELECT ProceedingGUID,CostGUID,ys_DeptAdjustDtl.Year,Month,ys_DeptAdjustDtl.AdjustAmount,deptguid FROM ys_DeptAdjustDtl left join ys_DeptAdjust on ys_DeptAdjustDtl.DeptAdjustGUID=ys_DeptAdjust.DeptAdjustGUID WHERE ys_DeptAdjustDtl.DeptAdjustGUID='" & Me._businessGUID & "'"
        dtTemp = MyDB.GetDataTable(strSQL)

        Try
            For i = 0 To dtTemp.Rows.Count - 1
                '调用存储过程同步更新
                UpdateYearPlan("调整", dtTemp.Rows(i).Item("DeptGUID").ToString, dtTemp.Rows(i).Item("ProceedingGUID").ToString, dtTemp.Rows(i).Item("CostGUID").ToString, dtTemp.Rows(i).Item("Year").ToString, dtTemp.Rows(i).Item("Month").ToString, dtTemp.Rows(i).Item("AdjustAmount").ToString)
            Next
        Catch ex As Exception
            Return "<xml result=""false"" errormessage=""审批失败，请稍后再试！""></xml>"
        End Try

        Return "<xml result=""true"" errormessage=""""></xml>"
    End Function

    '功能：不通过处理
    Public Overrides Function SaveData_NoPass() As String
        Dim MyPublicMng As New PublicMNG
        '获取审核人、审核日期的审核抬头信息
        Dim strApproveSeparator As String = MyPublicMng.GetApproveSeparator("审批不通过")
        Dim strSQL As String = "UPDATE ys_DeptAdjust SET ApproveState='未审核' " & _
                               ",Remarks='" & strApproveSeparator & Chr(13) & Chr(10) & "    工作流审批不通过" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "' + Convert(varchar(6000), IsNull(Remarks, '')) " & _
                               "WHERE DeptAdjustGUID='" & Me._businessGUID & "';"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return "<xml result=""false"" errormessage=""审批失败，请稍后再试！""></xml>"
        End If

        Return "<xml result=""true"" errormessage=""""></xml>"

    End Function
    '功能：作废
    Public Overrides Function SaveData_Cancel() As String
        Dim MyPublicMng As New PublicMNG
        '获取审核人、审核日期的审核抬头信息
        Dim strApproveSeparator As String = MyPublicMng.GetApproveSeparator("审批作废")
        Dim strSQL As String = "UPDATE ys_DeptAdjust SET ApproveState='未审核' " & _
                               ",Remarks='" & strApproveSeparator & Chr(13) & Chr(10) & "    工作流审批作废" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "' + Convert(varchar(6000), IsNull(Remarks, '')) " & _
                               " WHERE DeptAdjustGUID='" & Me._businessGUID & "';"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return "<xml result=""false"" errormessage=""审批失败，请稍后再试！""></xml>"
        End If

        Return "<xml result=""true"" errormessage=""""></xml>"
    End Function

    '功能：审批通过后再驳回数据处理
    Public Function SaveData_RollBack() As String
        Dim MyPublicMng As New PublicMNG
        '获取审核人、审核日期的审核抬头信息
        Dim strApproveSeparator As String = MyPublicMng.GetApproveSeparator("审批驳回")
        Dim strSQL As String = "UPDATE ys_DeptAdjust SET ApproveState='未审核' " & _
                               ",Remarks='" & strApproveSeparator & Chr(13) & Chr(10) & "    工作流审批驳回" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "' + Convert(varchar(6000), IsNull(Remarks, '')) " & _
                               " WHERE DeptAdjustGUID='" & Me._businessGUID & "';"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return "<xml result=""false"" errormessage=""审批失败，请稍后再试！""></xml>"
        End If

        Return "<xml result=""true"" errormessage=""""></xml>"

    End Function

    '功能：发起撤回
    Public Overrides Function InitiateWithDrawn() As String
        Dim MyPublicMng As New PublicMNG
        '获取审核人、审核日期的审核抬头信息
        Dim strApproveSeparator As String = MyPublicMng.GetApproveSeparator("审批不通过")
        Dim strSQL As String = "UPDATE ys_DeptAdjust SET ApproveState='未审核' " & _
                               ",Remarks='" & strApproveSeparator & Chr(13) & Chr(10) & "    工作流审批不通过" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "' + Convert(varchar(6000), IsNull(Remarks, '')) " & _
                               " WHERE DeptAdjustGUID='" & Me._businessGUID & "';"

        If MyDB.ExecSQL(strSQL) < 0 Then
            Return "<xml result=""false"" errormessage=""审批失败，请稍后再试！""></xml>"
        End If

        Return "<xml result=""true"" errormessage=""""></xml>"
    End Function

    Private Shared Sub UpdateYearPlan(ByVal strType As String, ByVal strDeptGUID As String, ByVal strProceedingGUID As String, ByVal strCostGUID As String, ByVal iYear As String, ByVal iMonth As String, ByVal decAdjustAmount As String)
        Dim myConnection As SqlConnection = New SqlConnection(MyDB.GetSqlConnectionString())
        Dim myCommand As SqlCommand = New SqlCommand("usp_Ys_UpdateYearPlanCost", myConnection)
        Dim MyAdapter As SqlDataAdapter
        Dim a_ds As New DataSet

        MyAdapter = New SqlDataAdapter
        myCommand.CommandType = CommandType.StoredProcedure
        MyAdapter.SelectCommand = myCommand

        Dim sqlParams As SqlParameterCollection = myCommand.Parameters

        sqlParams.Add(New SqlParameter("@Type", SqlDbType.NVarChar, 40))
        sqlParams.Add(New SqlParameter("@DeptGUID", SqlDbType.NVarChar, 40))
        sqlParams.Add(New SqlParameter("@ProceedingGUID", SqlDbType.NVarChar, 40))
        sqlParams.Add(New SqlParameter("@CostGUID", SqlDbType.NVarChar, 40))
        sqlParams.Add(New SqlParameter("@Year", SqlDbType.Int))
        sqlParams.Add(New SqlParameter("@Month", SqlDbType.Int))
        sqlParams.Add(New SqlParameter("@Amount", SqlDbType.Money))

        '参数赋值
        sqlParams("@Type").Value = strType
        sqlParams("@DeptGUID").Value = strDeptGUID
        sqlParams("@ProceedingGUID").Value = strProceedingGUID
        sqlParams("@CostGUID").Value = strCostGUID
        sqlParams("@Year").Value = iYear
        sqlParams("@Month").Value = iMonth
        sqlParams("@Amount").Value = decAdjustAmount

        Try
            MyAdapter.Fill(a_ds)

        Catch exc As Exception

        Finally
            myConnection.Close()
        End Try

    End Sub

End Class
